package com.example.yuejian11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.yuejian11.R;
import com.example.yuejian11.adapter.ClassAdapter;
import com.example.yuejian11.adapter.VideoAdapter;
import com.example.yuejian11.bean.ClassEntity;
import com.example.yuejian11.bean.ClubDetailEntity;
import com.example.yuejian11.bean.LikeEntity;
import com.example.yuejian11.bean.VideoEntity;
import com.example.yuejian11.common.Url;
import com.example.yuejian11.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ClubEntityDetail extends BaseActivity {


    private static final String TAG = "ActivityDetail";
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.tel)
    TextView tel;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.tx)
    ImageView tx;
    @BindView(R.id.masterintro)
    TextView masterintro;
    @BindView(R.id.mastername)
    TextView mastername;
    @BindView(R.id.title_activity)
    TitleView title_activity;
    LinearLayoutManager layoutManager;
    TextView rightTextTv;
    @BindView(R.id.recy)
    RecyclerView recyclerView;
    private Intent intent;
    private String cid;
    private RecyclerView.Adapter adapter;
    private Handler mhandler = new mHandler();
    private Context context;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.club_detail);
        super.onCreate(savedInstanceState);

        context = getBaseContext();
        initView();
        initDate();
    }

    private void initDate() {
        intent = getIntent();
        cid = intent.getIntExtra("cid", 1) + "";

//判断是否关注
        EasyHttp.get("yuejian6/public/index.php/json/club/isliked")
                .params("club_id", cid).params("user_id", uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 1; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {

                    }
                });


        EasyHttp.get("yuejian6/public/index.php/json/club/details")
                .params("club_id", cid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });


    }

    @OnClick(R.id.jingping)
    void setJingping() {

        EasyHttp.get("yuejian6/public/index.php/json/club/class_list")
                .params("club_id", cid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 4; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    @OnClick(R.id.xiangguan)
    void xiangguan() {

        EasyHttp.get("yuejian6/public/index.php/json/club/club_video")
                .params("club_id", cid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 5; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    private void initView() {
        layoutManager = new LinearLayoutManager(context);
        uid = SPUtils.getInstance().getInt("uid") + "";

        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(ClubEntityDetail.class);
            }
        });

        rightTextTv = title_activity.getRightTextTv();

        rightTextTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("关注".equals(rightTextTv.getText().toString())) {

                    EasyHttp.get("yuejian6/public/index.php/json/club/like")
                            .params("club_id", cid).params("user_id", uid)
                            .execute(new SimpleCallBack<String>() {
                                @Override
                                public void onSuccess(String data) {

                                    Message msg = Message.obtain(); // 实例化消息对象
                                    msg.what = 2; // 消息标识
                                    msg.obj = data; // 消息内容存放

                                    mhandler.sendMessage(msg);
                                }

                                @Override
                                public void onError(ApiException error) {

                                }
                            });

                } else {
                    EasyHttp.get("yuejian6/public/index.php/json/club/unlike")
                            .params("club_id", cid).params("user_id", uid)
                            .execute(new SimpleCallBack<String>() {
                                @Override
                                public void onSuccess(String data) {

                                    Message msg = Message.obtain(); // 实例化消息对象
                                    msg.what = 3; // 消息标识
                                    msg.obj = data; // 消息内容存放

                                    mhandler.sendMessage(msg);
                                }

                                @Override
                                public void onError(ApiException error) {

                                }
                            });
                }
            }
        });


    }

    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    Log.d(TAG, "handleMessage: " + msg.obj.toString());
                    ClubDetailEntity item = null;

                    try {
                        item =JSON.parseObject(msg.obj.toString(), ClubDetailEntity.class);

                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    name.setText("名字： " + item.getClub_name());
                    address.setText("地址： " + item.getAddress());
                    tel.setText("电话： " + item.getPhone());
                    //price.setText("价格："+item.get());
                    time.setText("时间：" + item.getOpentime());
                    Glide.with(context)
                            .load(Url.imgUrl + item.getPic())
                            .into(imageView);
                    masterintro.setText(item.getMaster_intro());
                    mastername.setText(item.getMaster_name());
                    Glide.with(context)
                            .load(Url.imgUrl + item.getMaster_pic())
                            .into(tx);

                    break;
                case 1:

                    LikeEntity islike = null;

                    try {
                        islike = JSON.parseObject(msg.obj.toString(), LikeEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if (islike.getError() == 0) {
                        rightTextTv.setText("已关注");
                    } else {
                        rightTextTv.setText("关注");
                    }

                    break;

                case 2:
                    LikeEntity like = null;
                    try {
                        like = JSON.parseObject(msg.obj.toString(), LikeEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if (like.getError() == 0) {
                        ToastUtils.showShort("关注成功");
                        rightTextTv.setText("已关注");
                    } else {
                        rightTextTv.setText("关注");
                    }
                    break;

                case 3:
                    LikeEntity unlike = null;
                    try {
                        unlike = JSON.parseObject(msg.obj.toString(), LikeEntity.class);

                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if (unlike.getError() == 0) {
                        ToastUtils.showShort("取消关注成功");
                        rightTextTv.setText("关注");
                    } else {
                        rightTextTv.setText("已关注");
                    }
                    break;
                case 4:
                    List<ClassEntity> classEntity = null;


                    try {
                        classEntity = JSON.parseArray(msg.obj.toString(), ClassEntity.class);

                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    adapter = new ClassAdapter(context, classEntity, R.layout.class_item);

                    //设置布局管理器
                    recyclerView.setLayoutManager(layoutManager);
                    //设置为垂直布局，这也是默认的
                    layoutManager.setOrientation(OrientationHelper.VERTICAL);
                    //设置Adapter
                    recyclerView.setAdapter(adapter);
                    //设置增加或删除条目的动画
                    recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());


                    break;
                case 5:
                    List<VideoEntity> videoEntities = null;
                    try {
                        videoEntities = JSON.parseArray(msg.obj.toString(), VideoEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    adapter = new VideoAdapter(context, videoEntities, R.layout.video_item);

                    //设置布局管理器
                    recyclerView.setLayoutManager(layoutManager);
                    //设置为垂直布局，这也是默认的
                    layoutManager.setOrientation(OrientationHelper.VERTICAL);
                    //设置Adapter
                    recyclerView.setAdapter(adapter);
                    //设置增加或删除条目的动画
                    recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());

                    break;

            }
        }
    }


}
